package com.eorchis.module.util;

import org.springframework.stereotype.Repository;

import sun.misc.BASE64Decoder;

import com.eorchis.utils.utils.PropertyUtil;
/**
 * 加密解密公共类
 * Title: Base64Util<br>
 * Description: <br>
 * Copyright @ 2014~2014 Goldgov .All rights reserved.<br>
 * 
 * @author ZhaoXX
 * @createDate 2014-11-26
 * @version $Revision: $
 */

@SuppressWarnings("unchecked")
@Repository(value = "com.eorchis.module.util.Base64Util")
public class Base64Util     
{     
    
    /**    
     * BASE64解密   
   * @param key          
     * @return          
     * @throws Exception          
     */              
    public static byte[] decryptBASE64(String key) throws Exception {               
        return (new BASE64Decoder()).decodeBuffer(key);               
    }               
                  
    /**         
     * BASE64加密   
   * @param key          
     * @return          
     * @throws Exception          
     */              
    public static String encryptBASE64(String s) throws Exception {               
        //return (new BASE64Encoder()).encodeBuffer(key);    
    	 return (new sun.misc.BASE64Encoder()).encode(s.getBytes("UTF-8"));
    }       
    public static void main(String[] args) throws Exception     
    {     
        String data = Base64Util.encryptBASE64("4028806b4887561d01488759669372");     
        System.out.println("加密前："+data);     
             
        byte[] byteArray = Base64Util.decryptBASE64(data);     
        System.out.println("解密后："+new String(byteArray)); 
        System.out.println(toChangeStr("5LmS5LmS5LmT5LmT"));
    }      
   
    /**
     * 转化后的字符串
     * @param str
     * @return
     * @throws Exception 
     */
    public static String toChangeStr(String str) throws Exception{
    	String retStr="";
    	if(PropertyUtil.objectNotEmpty(str)){
			byte[] byteArray = new Base64Util().decryptBASE64(str);
			retStr = new String(byteArray,"UTF-8");
    	}
    	return retStr;
    }
}    
